<template>
	<view>
	<view v-for="(equ,index) in device" :key="index" @click="zishebei(equ)">
		<view class="list" :class="{active:equ.isOnline==false}">
			<view class="image">
				<image :src="equ.iconUrl"></image>
			</view>
			<view class="text" v-if="equ.isOnline==true">{{equ.name}}</view>
	
			<view class="lixian_l" v-if="equ.isOnline==false">
				<view>{{equ.name}}</view>
				<view class="lixian">离线</view>
			</view>
			<view class="tiem" style="background-color: #ffeded;" v-if="equ.zigBeeWifi==false&&equ.times!=null&&equ.isOnline==true">
				<image src="../../static/image/mengling.png"></image>
				<!-- <view>07-30 10:46</view> -->
				<!-- <view><uni-section :title=" equ.dpsTime[101]" type="datetime"></uni-section></view> -->
				<view>{{equ.times}}</view>
			</view>
		</view>
	</view>
	</view>
</template>

<script>
	
	export default {
		data() {
			return {
				// jiegou: ["所有设备", "客厅", "主卧", "次卧", "餐厅", "厨房", "书房"],
				homeId: "",
				uniqueNo: "",
				device: [{
					time: '',
					times: '',
					open: '',
				}],
				family: [],
				uniqueCode:""
			}
		},
		onShow: function() { //option为object类型，会序列化上个页面传递的参数
			this.fangwu();
		},
		methods: {
			onBackPress(e){
				uni.switchTab({
					url:"/pages/index/index"
				})
				return true;
			},
			fangwu(){
				const uniqueCode = uni.getStorageSync('uniqueCode');
				// console.log(uniqueCode)
				this.uniqueNo=uniqueCode
				 this.getName();
			},
			// 加载家庭信息
			async getName() {
				try {
					uni.showLoading({
						title: '正在加载家庭记录'
					});
					// console.log(this.uniqueNo)
					const res = await this
						.$requestTuYa({
							// url: '/api-iot/home/' + this.uniqueNo + '/info',
							url:'/api-iot/home/' + this.uniqueNo + '/info',
							method: 'get'
						});
						// console.log("cacsd")

						// console.log(res.body.command.homeId)
					uni.hideLoading(); //加载结束
					if (res.success == true) {
						this.family = res.body.command;
						this.homeId = res.body.command.homeId
						// console.log(this.family)
						// console.log(this.homeId)
						this.getSheBei(this.family);
					} else { //加载失败
						uni.showToast({
							title: '家庭信息加载失败',
							duration: 2000
						});
					}
				} catch (e) { //如果涂鸦智能无法连接
					uni.hideLoading(); //加载结束
					uni.showToast({
						title: '家庭信息加载失败,请稍后再试',
						duration: 2000
					});
				}
			},
	
	// 		// 设备列表
			getSheBei(family) {
				console.log("dfsa")
				try {
					uni.showLoading({
						title: '正在加载设备列表'
					});
					const homeModule = uni.requireNativePlugin("LJY-HomeModule"); //调用android中自定义的插件名称
					const ret = homeModule.homeDetail({ //login表示自定义插件的函数名，第一个表示要传递的参数
						homeId: family.homeId
					});
					// console.log(ret)
					uni.hideLoading(); //加载结束
					if (ret.code == 0) { //加载成功
						this.device = ret.data.deviceList;
						for (var i = 0; i < this.device.length; i++) {
							this.device[i].time = this.device[i].dpsTime;
							this.device[i].open = this.device[i].dps;
							if (this.device[i].time[101] != undefined) {
								var newTime = new Date(this.device[i].time[101])
								var Moth = newTime.getMonth() + 1
								var mm = newTime.getDate()
								var every = newTime.getMinutes()
								Moth = Moth >= 10 ? Moth : "0" + Moth;
								mm = mm >= 10 ? mm : "0" + mm;
								every = every >= 10 ? every : "0" + every;
								var dataTime = Moth + "-" + mm + "" + newTime.getHours() + ":" + every
								// console.log(dataTime)
								this.device[i].times = dataTime
								// console.log(this.device[i].times)
							} else if (this.device[i].time[1] != undefined) {
								var newTime = new Date(this.device[i].time[1])
								var Moth = newTime.getMonth() + 1
								var mm = newTime.getDate()
								var every = newTime.getMinutes()
								Moth = Moth >= 10 ? Moth : "0" + Moth;
								mm = mm >= 10 ? mm : "0" + mm;
								every = every >= 10 ? every : "0" + every;
								var dataTime = Moth + "-" + mm + "" + newTime.getHours() + ":" + every
								// console.log(dataTime)
								this.device[i].times = dataTime
								// console.log(this.device[i].times)
							}
							// console.log(this.device)
						}
	
					} else { //加载失败
						uni.showToast({
							title: ret.msg,
							duration: 2000
						});
					}
				} catch (e) { //如果涂鸦智能无法连接
					uni.hideLoading(); //加载结束
					uni.showToast({
						title: '设备列表加载失败,请稍后再试',
					});
				}
			},
			// onNavigationBarButtonTap(e) {
			// 	// uni.navigateTo({
			// 	// 	url: '/pages/smartHome/home/networks?name=' + this.uniqueNo + '&homeId=' + this.homeId
			// 	// });
			// 	uni.showLoading({
			// 		title: '正在跳转涂鸦智能UI'
			// 	});
			// 	let message = "";
			// 	try {
			// 		// console.log(this.homeId);
			// 		const deviceConfigModule = uni.requireNativePlugin("LJY-DeviceConfigModule") //调用android中自定义的插件名称
			// 		//执行涂鸦智能登录,使用账户密码登录
			// 		const ret = deviceConfigModule.startDeviceActiveAction({ //login表示自定义插件的函数名，第一个表示要传递的参数
			// 			'homeId': this.homeId
			// 		});
			// 		uni.hideLoading(); //加载结束
			// 		message = ret.msg;
			// 	} catch (e) { //如果涂鸦智能无法连接
			// 		uni.hideLoading(); //加载结束
			// 		message = "涂鸦智能家居功能失效"
			// 	}
			// 	uni.showToast({
			// 		title: message,
			// 		mask: true,
			// 		duration: 2000
			// 	});
			// },
			//跳转设备控制面板
			zishebei(equ) {
				// console.log(equ.devId)
				
				// if(equ.zigBeeWifi==false){
					uni.showLoading({
						title: '正在跳转到涂鸦官方设备控制界面'
					});
					var str = String(this.homeId)
				
					try {
						// Activity设备控制面板跳转
						// //获取当前Activity
						// 	var str = String(this.homeId)
						// 	console.log(str)
						// 	//获取当前Activity
						// 	const main = plus.android.runtimeMainActivity();
						// 	//通过反射获取Android的Intent对象
						// 	const Intent = plus.android.importClass("android.content.Intent");
						// 	//通过宿主上下文创建 intent
						// 	const intent = new Intent(main.getIntent());
						// 	// io.dcloud.application.activity.DeviceControlPanelActivity
						// 	// com.lcvc.xgx.yyzxyq.activity.DeviceControlPanelModuleActivit
						// 	//设置要开启的Activity包类路径  com.lcvc.xgx.yyzxyq.activity.DeviceControlPanelActivity换掉你自己的界面
						// 	intent.setClassName(main, "com.lcvc.xgx.yyzxyq.activity.DeviceControlPanelModuleActivit");
						// 	//开启新的任务栈 （跨进程）
						// 	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						// 	//向原生界面传值操作
						// 	intent.putExtra("deviceId", equ.devId);
						// 	intent.putExtra("homeId",str);
						
						// 	//开启新的界面
						// 	main.startActivity(intent);
						// 	//请求码保证了，开始的新界面和返回的是同一个操作
						// 	var CODE_REQUEST = 1000
						// 	//采用startActivityForResult开启新的界面，当界面关闭时可以处理返回结果， CODE_REQUEST请求码是唯一标识
						// 	main.startActivityForResult(intent, CODE_REQUEST);
							// console.log("新页面已经加载出来")
						
						
						const module = uni.requireNativePlugin("LJY-DeviceManageModule") //调用android中自定义的插件名称
						//跳转到涂鸦设备详情
						module.goPanelWithCheckAndTip({ //login表示自定义插件的函数名，第一个表示要传递的参数
							deviceId: equ.devId,
							homeId: this.homeId
						});
						uni.hideLoading(); //加载结束
					} catch (e) { //如果涂鸦智能无法连接
						uni.hideLoading(); //加载结束
					}
				// }else{
				// 	// console.log(this.homeId)
				// 	uni.navigateTo({
				// 		url: '/pages/smartHome/home/eqList?devId=' + equ.devId + '&homeId=' + this.homeId+ '&uname=' + equ.name
				// 	});
				// }
				
				
	
	
			}
		}
	}
	
	
</script>

<style scoped lang="scss">
	.list {
		border-radius: 30rpx;
		// background-color: #007AFF;
		border-bottom: 3rpx solid #ebebeb;
		background-color: #FFFFFF;
		height: 160rpx;
		display: flex;
		width: 95%;
		margin-left: 20rpx;
		margin-top: 10rpx;
	}

	.list .image {
		image {
			margin: 30rpx 30rpx 0rpx 30rpx;
			height: 100rpx;
			width: 100rpx;
		}
	}

	.list .text {
		line-height: 160rpx;
		font-size: 35rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		font-weight: bold;
	}

	.list .tiem {
		display: flex;
		margin-top: 30rpx;
		margin-right: 30rpx;
		line-height: 90rpx;
		margin-left: auto;
		height: 90rpx;
		width: 200rpx;

		image {
			margin-top: 25rpx;
			margin-left: 20rpx;
			height: 40rpx;
			width: 40rpx;
		}

		view {
			margin-left: auto;
			margin-right: 30rpx;
			// background-color: #333333;
			color: #f0673d;
			width: 100rpx;
			font-size: 33rpx;
			// height: 90rpx;
			line-height: 40rpx;
			white-space: normal;
			word-break: break-all;
			word-wrap: break-word;
		}
	}

	.lixian_l {
		margin-top: 30rpx;
		font-size: 35rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		font-weight: bold;

		.lixian {
			width: 70rpx;
			margin-top: 10rpx;
			// height: 40rpx;
			border-radius: 10rpx;
			// margin-top: 60rpx;
			font-size: 30rpx;
			background-color: #52525c;
			color: #f2f1f4;
		}
	}

	.active {
		background-color: #ced5dc;
	}
</style>
